package com.example.sample.controller;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Properties;
import java.util.concurrent.Executor;


@RestController()
@RequestMapping("/confs")
public class ConfigurationCenterController {
    //日志组件
    private Logger log = LoggerFactory.getLogger(HellowController.class);

    @GetMapping("")
    public String Get() {

        try {
            ConfigService configService = GetConfigService();
            String content = configService.getConfig("testid", "DEFAULT_GROUP", 5000);
            String content1 = configService.getConfig("conf.yaml", "test_group", 5000);
            System.out.println(content);
            return "配置一：" + content + "配置二：" + content1;
        } catch (NacosException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return "Hello World";
    }

    private ConfigService GetConfigService(){
        String serverAddr = "1.117.193.193:8848";
        String dataId = "999";
        String group = "DEFAULT_GROUP";
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);

        ConfigService configService = null;
        try {
            configService = NacosFactory.createConfigService(properties);
        } catch (NacosException e) {
            e.printStackTrace();
        }
        return  configService;
    }

    @GetMapping("/post")
    public boolean Publish(){
        System.out.println("发布代码！！！！");
        String str ="{\"name\":\"张三\"}";
        ConfigService configService = GetConfigService();
        try {
            boolean b = configService.publishConfig("testpublish", "DEFAULT_GROUP", str,"json");
            return b;
        } catch (NacosException e) {
           // e.printStackTrace();
            System.out.println(e.getErrMsg());
        }
        return false;
    }

    @GetMapping("/remove")
    public boolean Removae(){
        ConfigService configService = GetConfigService();
        try {
            boolean b = configService.removeConfig("testpublish", "DEFAULT_GROUP");
            return b;
        } catch (NacosException e) {
            e.printStackTrace();
        }
        return false;
    }

    @GetMapping("/health")
    public void GetServerStatus(){
        ConfigService configService = GetConfigService();
        configService.getServerStatus();
    }

    @GetMapping("/lis")
    public void AddListener(){
        ConfigService configService = GetConfigService();
        Listener listener =new Listener() {
            @Override
            public Executor getExecutor() {
                System.out.println("getExecutor++++++++");
                return null;
            }

            @Override
            public void receiveConfigInfo(String s) {
                System.out.println(s);
            }
        };
        try {
            configService.addListener("testid", "DEFAULT_GROUP", listener);
        } catch (NacosException e) {
            e.printStackTrace();
        }
    }
}
